package com.kingdee.eas.scm.sm.pur.app;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

import org.apache.log4j.Logger;

import com.kingdee.bos.BOSException;
import com.kingdee.bos.Context;
import com.kingdee.bos.dao.IObjectCollection;
import com.kingdee.bos.dao.IObjectPK;
import com.kingdee.bos.dao.IObjectValue;
import com.kingdee.bos.framework.ejb.EJBFactory;
import com.kingdee.eas.common.EASBizException;
import com.kingdee.eas.scm.sm.pur.PurRequestEntryInfo;
import com.kingdee.eas.scm.sm.pur.PurRequestInfo;
import com.kingdee.util.db.SQLUtils;
public class PurRequestControllerBeanEx extends PurRequestControllerBean {
	 public PurRequestControllerBeanEx()
	    {
	    }

	    protected IObjectPK _SUBMIT(Context ctx, IObjectValue model)
	        throws BOSException, EASBizException
	    {
	        returnWrite(ctx, (PurRequestInfo)model);
	        return null;
	    }

	    public void returnWrite(Context context, PurRequestInfo purrequestinfo)
	        throws BOSException
	    {
	    }
	    public void _handleAfterTransform(Context ctx, String action, IObjectCollection collection)
	     throws EASBizException, BOSException
	    {
	    	PurRequestInfo info = null;
	        PurRequestEntryInfo entryInfo = null;
	        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd", Locale.US);
	        Date requirementDate = null;
	        Date proposeDeliveryDate = null;
	        Date proposePurchaseDate = null;
	        int i = 0;
	        for(int n = collection.size(); i < n; i++)
	        {
	            info = (PurRequestInfo)collection.getObject(i);
	            int j = 0;
	            for(int m = info.getEntries().size(); j < m; j++)
	            {
	                entryInfo = info.getEntries().get(j);
	                if(isBotpBill(info, "7UcZ6QEUEADgAAYawKg9C0Y+1VI=") && entryInfo.getRequirementDate() != null)
	                {
	                    requirementDate = entryInfo.getRequirementDate();
	                    Calendar calender = Calendar.getInstance();
	                    calender.setTime(requirementDate);
	                    calender.add(5, -1);
	                    simpleDateFormat.format(calender.getTime());
	                    proposeDeliveryDate = calender.getTime();
	                    calender.setTime(proposeDeliveryDate);
	                    calender.add(5, -1);
	                    simpleDateFormat.format(calender.getTime());
	                    proposePurchaseDate = calender.getTime();
	                    entryInfo.setProposeDeliveryDate(proposeDeliveryDate);
	                    entryInfo.setProposePurchaseDate(proposePurchaseDate);
	                }
	            }

	        }
        }
	    protected void _attachmentflag(Context ctx, IObjectValue model)
	        throws BOSException
	    {
	        Connection conn;
	        String tableName;
	        PurRequestInfo aPurRequestInfo;
	        PreparedStatement preStmt;
	        tableName = "T_SM_PurRequest";
	        aPurRequestInfo = (PurRequestInfo)model;
	        conn = null;
	        preStmt = null;
	        try
	        {
	            StringBuffer sqlBuff = new StringBuffer();
	            sqlBuff.append("Update ");
	            sqlBuff.append(tableName);
	            sqlBuff.append(" SET FAttachment = 1 ");
	            sqlBuff.append(" WHERE FID = ? ");
	            conn = EJBFactory.getConnection(ctx);
	            preStmt = conn.prepareStatement(sqlBuff.toString());
	            preStmt.setString(1, aPurRequestInfo.getId().toString());
	            preStmt.execute();
	        }
	        catch(SQLException ex)
	        {
	            throw new BOSException(ex);
	        }
	        SQLUtils.cleanup(preStmt, conn);	        
	    }

	    protected void _admeasure(Context context, String s)
	        throws BOSException, EASBizException
	    {
	    }

	    protected void _unadmeasure(Context context, String s)
	        throws BOSException, EASBizException
	    {
	    }

	    private static Logger logger = Logger.getLogger("com.kingdee.eas.scm.sm.pur.app.PurRequestControllerBeanEx");


}
